import pymysql
from datetime import datetime


# 获取数据库连接
def get_connection():
    host = '127.0.0.1'
    port = 3306
    user = 'root'
    password = 'root'
    database = 'virus'
    db = pymysql.connect(host, user, password, database, charset='utf8', port=port)
    return db


# 获取游标
def get_cursor(con):
    cursor = con.cursor()
    return cursor


# 关闭数据库连接
def close_connection(con):
    con.close()


def get_today_data(con, cursor, last_time):
    """
    查询当前疫情数据
    """
    sql = """
        select id, record_date, update_time
        from total_data
        where update_time = %s
        limit 1
    """
    cursor.execute(sql, (last_time,))
    res = cursor.fetchall()
    return res


def insert_today_data(con, cursor, record_date, last_time, today_total, today_add):
    """
    录入当天最新数据
    """
    sql = """
        insert into total_data 
        (record_date, total_confirm, total_suspect, total_dead, total_heal, update_time,
        add_confirm, add_suspect, add_dead, add_heal) 
        values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
    """
    cursor.execute(sql, (
        record_date, today_total['confirm'], today_total['suspect'], today_total['dead'],
        today_total['heal'], last_time,
        today_add['confirm'], today_add['suspect'], today_add['dead'], today_add['heal']))
    con.commit()


def delete_today_data(con, cursor, record_date):
    """
    删除指定日期数据
    """
    sql = """
        delete from total_data
        where record_date = %s
    """
    cursor.execute(sql, (record_date,))
    con.commit()


def get_province_today_data(con, cursor, last_time):
    """
    查询当前疫情数据
    """
    sql = """
        select id, record_date, update_time
        from province_data
        where update_time = %s
        limit 1
    """
    cursor.execute(sql, (last_time,))
    res = cursor.fetchall()  # fetchall 返回数据组成的元组，没有数据返回空元组
    return res


def delete_province_today_data(con, cursor, record_date):
    """
    删除省份指定日期数据
    """
    sql = """
        delete from province_data
        where record_date = %s
    """
    cursor.execute(sql, (record_date,))
    con.commit()


# 保存疫情统计数据
def save_day_data(con, cursor, day_total):
    sql = 'insert into total_data (record_date, total_confirm, total_suspect, total_dead, total_heal, update_time) values (%s, %s, %s, %s, %s, %s)'
    cursor.execute(sql, (
        '2020-' + day_total['date'].replace('.', '-'), day_total['confirm'], day_total['suspect'], day_total['dead'],
        day_total['heal'], datetime.now()))
    con.commit()


# 更新疫情数据
def update_day_data(con, cursor, day_add):
    sql = 'update total_data set add_confirm = %s, add_suspect = %s, add_dead = %s, add_heal = %s, update_time = %s where record_date = %s'
    cursor.execute(sql, (day_add['confirm'], day_add['suspect'], day_add['dead'], day_add['heal'], datetime.now(),
                         '2020-' + day_add['date'].replace('.', '-')))
    con.commit()


# 保存区域疫情数据
def save_province_data(con, cursor, record_date, province, last_time):
    sql = 'insert into province_data (record_date, province_name, total_confirm, update_time) values (%s, %s, %s, %s)'
    cursor.execute(sql, (record_date, province['name'], province['total']['confirm'],last_time))
    con.commit()
